package com.wcl.demo.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupInfo;
import com.wcl.demo.R;
import com.wcl.demo.ui.fragment.ItemInterface.GroupOnItemListener;

import java.security.acl.Group;
import java.util.List;

/**
 * Created by dllo on 15/11/22.
 */
public class GroupRecylcerViewAdapter extends RecyclerView.Adapter<GroupRecylcerViewAdapter.GroupRecylcerViewHolder> {

    private List<EMGroup> grouplist;
    private GroupOnItemListener listener;

    public void addData(List<EMGroup> grouplist) {
        this.grouplist = grouplist;
        notifyDataSetChanged();
    }

    public void setGroupItemClickListener(GroupOnItemListener listener){
        this.listener = listener;
    }

    class GroupRecylcerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView friends;
        private RelativeLayout relativeLayout;
        private int position;

        public GroupRecylcerViewHolder(View itemView) {
            super(itemView);
            friends = (TextView) itemView.findViewById(R.id.friends);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.group_relative);
            relativeLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                String groupId = grouplist.get(position).getGroupId();
                EMGroup emGroup=grouplist.get(position);
                listener.setOnGroupOnItemListener(emGroup , position);
            }
        }
    }

    @Override
    public GroupRecylcerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recyclerview_friends_item, null);
        return new GroupRecylcerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(GroupRecylcerViewHolder holder, int position) {
        holder.friends.setText(grouplist.get(position).getGroupName());
        holder.position = position;
    }

    @Override
    public int getItemCount() {
        return grouplist != null && grouplist.size() > 0 ? grouplist.size() : 0;
    }
}
